package com.hw.one.dam2.controller;

import com.hw.one.core.controller.BaseController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.service.ICountyGridService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 县级网格化控制层类
 * <p>
 * Copyright 2017 汉威.智慧环保事业部
 * Created by 毛超然 on 2017/6/28 0009.
 */

@RestController
@RequestMapping(value = "countyGrid")
public class CountyGridController extends BaseController {

    @Autowired
    private ICountyGridService countyGridService;

    /**
     * 获取县级的乡镇、村组信息
     *
     * @param model
     * @param request
     * @return
     */

    @RequestMapping(value = "findCountGridRegionData", method = RequestMethod.GET)
    public MsgResult findCountGridRegionData(Model model, HttpServletRequest request) {
        String level = request.getParameter("level");
        if (StringUtils.isEmpty(level)) {
            return MsgResult.errorResult("村镇级别不能为空");
        }
        // 根据名字的模糊搜索
        String regionName = request.getParameter("regionName");
        if (null == regionName) {
            regionName = "";
        }
        try {
            //regionName = new String(regionName .getBytes("iso8859-1"),"utf-8");
            regionName = URLDecoder.decode(regionName, "UTF-8");
            Map<String, String> paraMap = new HashMap<String, String>();
            paraMap.put("level", level);
            paraMap.put("regionName", regionName);
            List<Map> regionListMap = countyGridService.findSubRegionDataByLevel(paraMap);
            return MsgResult.okResult(regionListMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询县级村镇信息异常,后台异常", e);
            return MsgResult.errorResult();
        }
    }

    /**
     * 获取县级的乡镇、村组详细信息
     *
     * @param model
     * @param request
     * @return
     */

    @RequestMapping(value = "findCountGridRegionDetailData", method = RequestMethod.GET)
    public MsgResult findCountGridRegionDetailData(Model model, HttpServletRequest request) {
        String regionCode = request.getParameter("regionCode");
        if (StringUtils.isEmpty(regionCode)) {
            return MsgResult.errorResult("区域编码不能为空");
        }
        try {
            Map<String, List<Map>> memberListMap = countyGridService.findGridMemberListMapByParentCode(regionCode);
            return MsgResult.okResult(memberListMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询县级村镇详细信息异常,后台异常", e);
            return MsgResult.errorResult();
        }
    }

    /**
     * 获取当前登录用户区域下的企业信息列表
     *
     * @param request
     * @return
     */

    @RequestMapping(value = "findEnterpriseListByRegionCode", method = RequestMethod.GET)
    public MsgResult findEnterpriseListByRegionCode(HttpServletRequest request) {
        try {
            List<Map<String, Object>> memberListMap = countyGridService.findEnterpriseListByRegionCode();
            return MsgResult.okResult(memberListMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询当前登录用户区域下的企业信息列表,后台异常", e);
            return MsgResult.errorResult();
        }
    }

}

